package com.bite.maven_demo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/JSON")
@RestController
public class JSON {
    private static ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) throws JsonProcessingException {
        Person person = new Person();
        person.setId(5);
        person.setName("zhangsan");
        person.setPassword("123456");
        //对象转为JSON字符串
        String jsonStr = objectMapper.writeValueAsString(person);
        System.out.println("JSON字符串为："+jsonStr);
        //JSON字符串转为对象
        Person p = objectMapper.readValue(jsonStr,Person.class);
        System.out.println("转换的对象id："+p.getId()+",name:"+p.getName()+",password"+p.getPassword());
    }
    @RequestMapping(value = "/m7")
    public Object method7(@RequestBody Person person) {
        return person.toString();
    }
    @RequestMapping("/m8/{id}/{name}")
    public String method8(@PathVariable Integer id,@PathVariable("name") String username) {
        return "解析参数id："+id+",name: "+username;
    }
    @RequestMapping("/getCookie")
    public String cookie(@CookieValue("bite") String bite) {
        return "bite: " + bite;
    }
}
